﻿using Framework;
using Protocol.Code;
using UnityEngine;
using UnityEngine.UI;

namespace PokerGame
{


    public class CreatePanel : UIBase
    {
        private InputField inputName;
        private Button btnCreate;

        private PromptMsg prompt;
        private SocketMessage message;

        private void Awake()
        {
            Bind(UIEvent.CREATE_PANEL_ACTIVE);
            inputName = transform.Find("inputName").GetComponent<InputField>();
            btnCreate = transform.Find("btnCreate").GetComponent<Button>();
        }

        void Start()
        {
            btnCreate.onClick.AddListener(BtnCreate_OnClick);
            prompt = new PromptMsg();
            message = new SocketMessage();
        }

        public override void OnDestroy()
        {
            btnCreate.onClick.RemoveListener(BtnCreate_OnClick);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.CREATE_PANEL_ACTIVE:
                    SetPanelActive((bool)message);
                    break;
                default:
                    break;
            }
        }

        private void BtnCreate_OnClick()
        {
            if (string.IsNullOrEmpty(inputName.text))
            {
                //非法输入
                prompt.Change("请正确输入您的名称", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, prompt);
                return;
            }

            //自身处理：进行一些其他的判断 比如 长度 符号。。。

            //向服务器发送一个创建的请求
        }

    }
}